package com.lucian.ge.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lucian.ge.user.mapper.UserMapper;
import com.lucian.ge.comman.model.ResultMap;
//import com.lucian.ge.user.model.ResultMap;
import com.lucian.ge.util.JWTUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @author lucian
 * @since 2019-04-17
 */
@Api(value = "UserController", description = "用户鉴权中心")
@RestController
@RequestMapping("/user")
public class UserController {
    private final UserMapper userMapper;
    private final ResultMap resultMap;

    @Autowired
    public UserController(UserMapper userMapper, ResultMap resultMap) {
        this.userMapper = userMapper;
        this.resultMap = resultMap;
    }


    /**
     * 检查登录状态
     */

    @ApiOperation(value="用户登录状态", notes="user/token")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })

    @GetMapping("/islogin")
    @RequiresRoles("user")
//    header user
    public ResultMap islogin() {
        return resultMap.success().code(200).message("登录成功！");
    }


    /**
     * 拥有 user, admin 角色的用户可以访问下面的页面
     */
    @GetMapping("/getMessage")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })
    @RequiresRoles(logical = Logical.OR, value = {"user", "admin"})
    public ResultMap getMessage() {
        return resultMap.success().code(200).message("成功获得信息！");
    }

    @PostMapping("/updatePassword")
    @RequiresRoles(logical = Logical.OR, value = {"user", "admin"})
    public ResultMap updatePassword(String username, String oldPassword, String newPassword) {
        String dataBasePassword = userMapper.getPassword(username);
//        System.out.println(dataBasePassword);
//        System.out.println(oldPassword);
//        System.out.println(newPassword);
//        System.out.println(dataBasePassword.equals(oldPassword));
        if (dataBasePassword.equals(oldPassword)) {
//            System.out.println("newPassword"+ newPassword);

            userMapper.updatePassword(username, newPassword);
            System.out.println("12");
        } else {
            return resultMap.fail().message("密码错误！");
        }
        return resultMap.success().code(200).message("修改密码成功");
    }

    /**
     * 拥有 user, admin 角色 或 vip, normal 权限可以访问该页面
     */
    @GetMapping("/getVipMessage")
    @RequiresRoles(logical = Logical.OR, value = {"user", "admin"})
    @RequiresPermissions(logical = Logical.OR, value = {"vip", "normal"})
    public ResultMap getVipMessage() {
        return resultMap.success().code(200).message("成功获得 vip 信息！");
    }


    /**
     * 获取用户信息
     */
    @GetMapping("/getUserInfo")
    @RequiresRoles("user")
    public ResultMap getUserInfo() {
        return resultMap.success().code(200).message("成功获得用户信息！");
    }


//    鉴权中心
    /**
     * 检查普通用户登录状态
     */

    @ApiOperation(value="普通注册用户登录状态", notes="user/token")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })

    @PostMapping("/checkuser")
    @RequiresRoles("user")
    public ResultMap check(@RequestHeader String token) {

//        if (!JWTUtil.getUsername(token).equals(user)) {
//            return resultMap.fail().code(401).message("普通注册用户非法请求").token("").res("").data(null);
//        }

        return resultMap.success().code(200).message("普通注册用户登录成功！").token("").res("").data(null);
    }


    /**
     * 检查管理员登录状态
     */

    @ApiOperation(value="管理员登录状态", notes="user/token")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })

    @PostMapping("/checkadmin")
    @RequiresRoles("admin")
    public ResultMap checkadmin(@RequestHeader String token) {

//        if (!JWTUtil.getUsername(token).equals(user)) {
//            return resultMap.fail().code(401).message("管理员非法请求").token("").res("").data(null);
//        }

        return resultMap.success().code(200).message("管理员登录成功！").token("").res("").data(null);
    }


    /**
     * 检查区长登录状态
     */

    @ApiOperation(value="区长登录状态", notes="user/token")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })

    @PostMapping("/checkpartner")
    @RequiresPermissions(logical = Logical.OR, value = {"warden", "leader"})
    public ResultMap checkwardenAndLeader(@RequestHeader String token) {

//        if (!JWTUtil.getUsername(token).equals(user)) {
//            return resultMap.fail().code(401).message("管理员非法请求").token("").res("").data(null);
//        }

        return resultMap.success().code(200).message("合伙人登录成功！").token("").res("").data(null);
    }

}
